package com.blog.Bard.base.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blog.Bard.base.dao.BardMenuDao;
import com.blog.Bard.base.pojo.BardMenu;
import com.blog.Bard.common.service.BaseServiceImpl;
import com.blog.Bard.common.util.Pager;

@Service
@Transactional
public class BardMenuServiceImpl extends BaseServiceImpl implements BardMenuService {
	
	@Autowired
	private BardMenuDao bardmenuDao;

	@Override
	public List<Map<String, Object>> getMenuList() throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<BardMenu> parentList = bardmenuDao.getParentMenuList();
		for(int i=0;i<parentList.size();i++) {
			BardMenu parentMenu = parentList.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("level", parentMenu.getLevel());
			map.put("name", parentMenu.getMenuName());
			map.put("code", parentMenu.getMenuCode());
			map.put("url", parentMenu.getUrl());
			List<Map<String, Object>> childList = bardmenuDao.getChildMenuList(parentMenu.getMenuCode());
			map.put("childs", childList);
			result.add(map);
		}
		return result;
	}

	@Override
	public Pager getMenuPager(Map<String, Object> params, int pageNo, int pagesize) throws Exception {
		List<Object> args = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * \n");
		sql.append(" ,(case when status = 1 then '有效' when status = 0 then '无效' else '' end) statusStr \n");
		sql.append(" from menu where 1 = 1 \n");
		Integer status = (Integer)params.get("status");
		String searchValue = (String)params.get("searchValue");
		if(status!=-1) {
			sql.append(" and status = ? \n");
			args.add(status);
		}
		if(!"".equals(searchValue)) {
			sql.append(" and (locate(?, menuName)>0 or locate(?, menuCode)>0) \n");
			args.add(searchValue);
			args.add(searchValue);
		}
		sql.append(" order by seq ");
		return bardmenuDao.getPagerBySql(sql.toString(), pageNo, pagesize, args.toArray());
	}

	@Override
	public List<Map<String, Object>> getMenuTree() throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<BardMenu> parentList = bardmenuDao.getParentMenuList();
		for(int i=0;i<parentList.size();i++) {
			BardMenu parentMenu = parentList.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", parentMenu.getId());
			map.put("title", parentMenu.getMenuName());
			List<Map<String, Object>> children = bardmenuDao.getChildMenuTrees(parentMenu.getMenuCode());
			map.put("children", children);
			result.add(map);
		}
		return result;
	}

	@Override
	public BardMenu getOneById(Integer id) throws Exception {
		if(id==0) {
			return null;
		}
		return bardmenuDao.getOne(id);
	}

	@Override
	public List<BardMenu> getParentMenuList() throws Exception {
		return bardmenuDao.getParentMenuList();
	}

	@Override
	public void save(BardMenu t) throws Exception {
		if(t.getSeq()==null) {
			Map<String,Object> map = bardmenuDao.getNextSeq(t.getParentCode());
			Object nextSeq = map.get("nextSeq");
			t.setSeq(Integer.parseInt(nextSeq.toString()));
		}
		bardmenuDao.save(t);
	}

	@Override
	public void del(Integer id) throws Exception {
		bardmenuDao.deleteById(id);
	}

}
